package com.dev.security.manager.controllers;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.security.manager.beans.Role;
import com.dev.security.manager.service.IRoleService;
import com.dev.security.util.DataGrid;
import com.dev.security.util.Response;

/**
 * @author 潜行的青衣
 * @see
 */
@Controller
@RequestMapping("/manager/role/")
public class RoleController {

	@Resource
	private IRoleService roleService;
	
	@RequestMapping("browse")
	public @ResponseBody DataGrid<Role> browse() {
		return new DataGrid<Role>(this.roleService.findAll());
	}
	
	@RequestMapping("create")
	public @ResponseBody Response create(Role role) {
		if(this.roleService.create(role)) {
			return new Response(true);
		} else {
			return new Response("此角色已经存在！");
		}
	}
	
	@RequestMapping("update")
	public @ResponseBody Response update(Role role) {
		this.roleService.update(role);
		return new Response(true);
	}
	
	@RequestMapping("remove")
	public @ResponseBody Response remove(String[] ids) {
		this.roleService.remove(ids);
		return new Response(true);
	}
	
	@RequestMapping("tree")
	public @ResponseBody List<Map<String, Object>> tree(String role_id) {
		return this.roleService.buildAppPermTree(role_id);
	}
	
	@RequestMapping("alloc")
	public @ResponseBody Response alloc(String role_id, String[] app_ids, String[] per_ids) {
		this.roleService.saveAppUnit(role_id, app_ids);
		this.roleService.savePermUnit(role_id, per_ids);
		return new Response(true);
	}
}
